﻿using Business.Base.DB.Query;
using Business.Base.Model;
using Business.Base.Service;
using Business.Base.Service.Create;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pumptester.ExperimentForms.Base
{
    public partial class BaseViewForm : Form
    {
        ExperimentService service;
        Experiment experiment;
        private IChartDrawer chartDrawer;
        private Form preForm;
        private BaseChartData chartData;
        ExperimentService experimentService;

        protected virtual ExperimentService ExperimentService { get { return experimentService; } }

        public IChartDrawer ChartDrawer { get => chartDrawer; set => chartDrawer = value; }
        public ExperimentService Service { get => service; }
        public Experiment Experiment { get => experiment; set => experiment = value; }      
        public Form PreForm { get => preForm; set => preForm = value; }
        public BaseChartData ChartData { get => chartData; set => chartData = value; }

        public BaseViewForm()
        {
            InitializeComponent();
        }


        public BaseViewForm(Experiment experiment, BaseChartData chart ,Form preForm)
        {
            InitializeComponent();
            this.Experiment = experiment;
            this.preForm = preForm;
            this.ChartData = chart;
            this.experimentService = ServiceContainer.getInstance()[this.experiment.ExperimentType];
        }

        protected virtual void updateExperimentInfo()
        { 
        }


        private void LoadExperiment(String code)
        {
            ExperimentQuery query = new ExperimentQuery();
            query.Code = code;
            query.OrderBy = "Id";
            query.PageNum = 1;
            query.PageSize = 1;
            query.OrderType = "desc";

            //获取试验的基础数据
            //DataTable table = service.QueryExperiment(query);
            List<Object> experimentList = service.QueryExperiment(query);
            Experiment exp = null;
            if (experimentList.Count == 1)
            {
                exp = (Experiment)experimentList[0];
            }
            else
            {
                MessageBox.Show("数据错误，无法查到准确的数据", "提示", MessageBoxButtons.OK);
                this.Close();
                return;
            }


            ExperimentRecordQuery recordQuery = new ExperimentRecordQuery();
            recordQuery.PageNum = 1;
            recordQuery.PageSize = 100;
            recordQuery.ExperimentCode = exp.ExperimentCode;
            List<Object> recordList = service.QueryExperimentRecord(recordQuery);
            foreach (Object record in recordList)
            {
                exp.RecordList.Add((ExperimentRecord)record);
            }
            this.Experiment = exp;

        }

        private void toolStripExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

        protected virtual void LoadExperiment()
        {

        }

        protected virtual void LoadExperimentInfo()
        {

        }

        private void toolStripExportData_Click(object sender, EventArgs e)
        {
            this.updateExperimentInfo();
            this.ExperimentService.CaculateExperimentResult( ref this.experiment);
            //保存实验结果到Excel文件，Excel文件在原来的基础上添加一个日期后缀,
            //去掉原来文名中的后缀，再加上日期后，再加后缀.xlsx
     
            String fileName = Path.GetDirectoryName(this.Experiment.DefaultSavePath)+Path.DirectorySeparatorChar+ Path.GetFileNameWithoutExtension(this.Experiment.DefaultSavePath) + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
            this.ExperimentService.SaveExperimentReport(this.Experiment, fileName);
            if (Experiment.FilePath != null)
            {
                System.Diagnostics.Process.Start(fileName);
            }
        }

        private void BaseViewForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.preForm.Show();
        }
    }
}
